package engine.game.hud;

import engine.game.Tickable;
import engine.net.chat.Chat;
import engine.net.chat.ChatClient;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ChatBox extends JTabbedPane implements Tickable {

    private static final long serialVersionUID = 1L;
    
    private static final String[] mpChatMethodOptions = {
        "Start Local Server",
        "Connect to Remote Server"
    };

    private final JButton inputButton;
    private final JTextArea textArea;
    private final JScrollPane textScroll;
    private final JTextField inputField;

    private final JPanel chatPanel;
    private final JPanel boxPanel;
    private final JPanel inputPanel;
    private final JPanel inputFieldWrapper;

    private final JPanel mpPanel;
    private final JLabel mpLabel;
    private final JPanel mpLabelWrapper;
    private final JLabel mpChatLabel;
    private final JPanel mpChatLabelWrapper;
    private final JLabel mpCurrentChatServerLabel;
    private final JPanel mpCurrentChatServerLabelWrapper;
    private final JPanel mpCurrentChatServerPanel;
    private final JComboBox mpChatMethod;
    private final JPanel mpChatMethodWrapper;
    
    private final JPanel optionsPanel;

    private final Font titleFont = new Font("Arial", Font.BOLD, 16);
    private final Font subTitleFont = new Font("Arial", Font.BOLD, 12);
    private final Font wordFont = new Font("Arial", Font.PLAIN, 12);
    
    // Functional variables
    private ChatClient chatClient;
    private Chat chat;

    private final ChatBoxListener listener;

    public ChatBox() {

        // General Definitions
        GridBagConstraints c;
        listener = new ChatBoxListener();

        // Chat Tab setup
        chatPanel = new JPanel();
        chatPanel.setLayout(new GridBagLayout());

        textArea = new JTextArea(20, 20);
        textArea.setFont(wordFont);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setEditable(false);
        textArea.setBackground(Color.WHITE);
        textArea.setText("This is where chat messages and combat notifications go.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nHello.");
        textScroll = new JScrollPane(textArea);
        boxPanel = new JPanel(new BorderLayout());
        boxPanel.add(textScroll, BorderLayout.CENTER);

        boxPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 1;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.fill = GridBagConstraints.BOTH;
        chatPanel.add(boxPanel, c);

        inputPanel = new JPanel();
        inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.LINE_AXIS));

        inputField = new JTextField(20);
        inputField.setBackground(Color.WHITE);
        inputField.setFont(wordFont);
        inputField.setText("INPUT STUFF HERE");

        inputFieldWrapper = new JPanel();
        inputFieldWrapper.setLayout(new BoxLayout(inputFieldWrapper, BoxLayout.PAGE_AXIS));
        inputFieldWrapper.add(Box.createVerticalGlue());
        inputFieldWrapper.add(inputField);
        inputFieldWrapper.add(Box.createVerticalGlue());

        inputPanel.add(inputFieldWrapper);

        inputButton = new JButton("Send");
        inputButton.addActionListener(listener);
        inputPanel.add(inputButton);

        inputPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 2;
        c.weightx = 0.0;
        c.weighty = 0.0;
        c.fill = GridBagConstraints.BOTH;
        chatPanel.add(inputPanel, c);

        this.addTab("Chat", chatPanel);

        // Multiplayer Tab Setup
        mpPanel = new JPanel();
        mpPanel.setLayout(new GridBagLayout());
        mpLabel = new JLabel("Multiplayer Settings");
        mpLabel.setFont(titleFont);
        mpLabel.setHorizontalTextPosition(JLabel.CENTER);
        
        mpLabelWrapper = new JPanel();
        mpLabelWrapper.setLayout(new BoxLayout(mpLabelWrapper, BoxLayout.LINE_AXIS));
        mpLabelWrapper.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        mpLabelWrapper.add(mpLabel);
        
        mpChatLabel = new JLabel("Chat Server");
        mpChatLabel.setFont(subTitleFont);
        mpChatLabel.setHorizontalTextPosition(JLabel.LEFT);
        mpChatLabelWrapper = new JPanel();
        mpChatLabelWrapper.setLayout(new BoxLayout(mpChatLabelWrapper, BoxLayout.LINE_AXIS));
        mpChatLabelWrapper.setBorder(BorderFactory.createEmptyBorder(3, 5, 3, 5));
        mpChatLabelWrapper.add(mpChatLabel);
        mpChatLabelWrapper.add(Box.createHorizontalGlue());
        
        mpCurrentChatServerLabel = new JLabel("Current Server: " + "NONE");
        mpCurrentChatServerLabel.setHorizontalTextPosition(JLabel.LEFT);
        mpCurrentChatServerLabel.setFont(wordFont);
        mpCurrentChatServerLabelWrapper = new JPanel();
        mpCurrentChatServerLabelWrapper.setLayout(new BoxLayout(mpCurrentChatServerLabelWrapper, BoxLayout.LINE_AXIS));
        mpCurrentChatServerLabelWrapper.add(mpCurrentChatServerLabel);
        mpCurrentChatServerLabelWrapper.add(Box.createHorizontalGlue());
        mpCurrentChatServerLabelWrapper.setBorder(BorderFactory.createEmptyBorder(0, 15, 3, 5));
        mpCurrentChatServerPanel = new JPanel();
        mpCurrentChatServerPanel.setLayout(new BoxLayout(mpCurrentChatServerPanel, BoxLayout.PAGE_AXIS));
        mpCurrentChatServerPanel.add(mpCurrentChatServerLabelWrapper);
        
        mpChatMethod = new JComboBox(mpChatMethodOptions);
        mpChatMethodWrapper = new JPanel();
        mpChatMethodWrapper.setLayout(new BoxLayout(mpChatMethodWrapper, BoxLayout.LINE_AXIS));
        mpChatMethodWrapper.setBorder(BorderFactory.createEmptyBorder(0, 15, 3, 5));
        mpChatMethodWrapper.add(mpChatMethod);
                
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1.0;
        c.weighty = 0.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.NORTHWEST;
        mpPanel.add(mpLabelWrapper, c);
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 1;
        c.weightx = 1.0;
        c.weighty = 0.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.NORTHWEST;
        mpPanel.add(mpChatLabelWrapper, c);
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 2;
        c.weightx = 1.0;
        c.weighty = 0.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.NORTHWEST;
        mpPanel.add(mpCurrentChatServerPanel, c);
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 3;
        c.weightx = 1.0;
        c.weighty = 0.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.NORTHWEST;
        mpPanel.add(mpChatMethodWrapper, c);
        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 3;
        c.weightx = 1.0;
        c.weighty = 1.0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.NORTHWEST;
        mpPanel.add(Box.createVerticalGlue(), c);
        this.addTab("Multiplayer", mpPanel);

        // Options Tab Setup
        optionsPanel = new JPanel();
        optionsPanel.setLayout(new BoxLayout(optionsPanel, BoxLayout.PAGE_AXIS));
        optionsPanel.add(new JLabel("Options"));
        this.addTab("Options", optionsPanel);
    }

    @Override
    public void tick() {
        // Do things
    }

    private class ChatBoxListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            Object source = e.getSource();

            if (source == inputButton) {
                
            }
        }
    }
}
